﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using System.Dynamic;

namespace CVsharp.Jquery.Tree
{
    public class Tree : ITree
    {
        public IList<Node> RootNodes { get; set; }
        public Tree()
        {
            RootNodes = new List<Node>();
        }
        /// <summary>
        /// 根据Json字符串转化为Tree类型
        /// </summary>
        /// <param name="json"></param>
        /// <param name="nodeStr"></param>
        /// <param name="childrenStr"></param>
        /// <returns></returns>
        public static Tree Parse(string json, string nodeStr, string childrenStr)
        {
            var t = new Tree
                        {
                            RootNodes = JsonToNodes(JArray.Parse(json), childrenStr)
                        };
            return t;

        }
        private static IList<Node> JsonToNodes(IEnumerable<JToken> jarray, string childrenStr)
        {
            IList<Node> nodes = new List<Node>();
            foreach (var jobject in jarray)
            {
                JObject jo = jobject.ToObject<JObject>();
                Node node = new Node();
                foreach (var propert in jo.Properties())
                {
                    node.Propertys.Add(propert.Name, propert.Value);
                }
                if ((jo[childrenStr] != null && jo[childrenStr].Any()))
                {
                    node.Nodes = JsonToNodes(jo[childrenStr].ToObject<JArray>(), childrenStr);
                }
                nodes.Add(node);
            }
            return nodes;
        }
        public string ToJsonString()
        {
            return ToJsonString("name", "children");
        }
        public string ToJsonString(string nodeStr, string childrenStr)
        {
            JObject RootNode = new JObject();
            return AddNode(RootNode, RootNodes, childrenStr).ToString();
        }
        /// <summary>
        /// 递归添加节点
        /// </summary>
        /// <param name="jO">json节点</param>
        /// <param name="Nodes"> 子树节点</param>
        /// <param name="childrenStr">子群节点命名</param>
        /// <returns></returns>
        private JArray AddNode(JObject jO, IEnumerable<Node> Nodes, string childrenStr)
        {
            JArray jarray = new JArray();
            foreach (var n in Nodes)
            {
                JObject jOTmep = new JObject();
                foreach (var p in n.Propertys)
                {
                    jOTmep.Add(new JProperty(p.Key, p.Value.ToString()));
                }
                if (n.Nodes != null && n.Nodes.Any())
                {
                    AddNode(jOTmep, n.Nodes, childrenStr);
                }
                jarray.Add(jOTmep);
            }
            jO.Add(childrenStr, jarray);
            return jarray;
        }
    }
}
